const { Backend } = require('dx-router-admin');

module.exports = class Config extends Backend {
	constructor(ctx) {
		super(ctx);
	}
	async add(param) {
		param.sort = isNaN(Number(param.sort)) ? 0 : Number(param.sort);
		return super.add(param);
	}
	async edit(param) {
		param.sort = isNaN(Number(param.sort)) ? 0 : Number(param.sort);
		return super.edit(param);
	}
	/**
	 * 保存配置
	 * @param {Object} param
	 */
	async save(param) {
		let keys = Object.keys(param);
		let list = await this.currentService.where('name', 'in', keys).select(true);
		if (!list.length) {
			return this.$dx.fail(this.$dx.$t('未找到有效配置'));
		}
		await Promise.all(
			list.map(async (item) => {
				await this.currentService.save({
					_id: item._id,
					value: param[item.name],
				});
			})
		);
		return this.$dx.success(this.$dx.$t('保存成功'));
	}
};
